<?php

class Reservation extends CI_Controller {

    function __construct() {
        parent::__construct();
        $level_id = $this->session->userdata('logged_in');
        if ($level_id['level_id'] == null) {
            redirect('user/login', 'refresh');
        }
        if ($level_id['level_id'] == 0) {
            redirect('user', 'refresh');
        }
    }

    function index() {
        $crud = new grocery_CRUD();
        $crud->set_table('reservation');
        $crud->set_subject('Reservation');
        $crud->set_relation('username', 'user', 'staff_name');
        $crud->set_relation('guest_id', 'guest', 'guest_name');
        $crud->columns('resv_id', 'guest_id', 'resv_status', 'room_number', 'resv_booking_time', 'resv_checkin', 'resv_checkout', 'resv_total_price', '_');
        $crud->display_as('guest_id', 'Guest Name');
        $crud->display_as('resv_status', 'Status');
        $crud->display_as('room_number', 'Room Number');
        $crud->display_as('resv_booking_time', 'Date of Booking');
        $crud->display_as('resv_checkin', 'Date Check In');
        $crud->display_as('resv_checkout', 'Date Check Out');
        $crud->order_by('resv_booking_time', 'desc');

        $crud->callback_column('_', array($this, 'editResv'));

        $crud->unset_add();
        $crud->unset_edit();
        $crud->unset_read();
        $crud->unset_delete();

        $output = (array) $crud->render();
        $output['title'] = 'Reservation';

        $this->load->template('reservation/index', $output);
    }

    public function editResv($value, $row) {
        return '<a style="color:white" class="btn btn-warning" href="' . base_url() . 'reservation/edit/' . $row->resv_id . '">Edit</a>
                <a style="color:white" class="btn btn-warning" href="' . base_url() . 'reservation/service/' . $row->resv_id . '">Service</a>
                <a style="color:white" class="btn btn-success" href="' . base_url() . 'reservation/pay/' . $row->resv_id . '">Pembayaran</a>';
    }

    function payment_type() {
        $crud = new grocery_CRUD();
        $crud->set_table('payment_type');
        $crud->set_subject('Tipe Payment');
        $crud->required_fields('payment_id', 'payment_name');
        $crud->columns('payment_id', 'payment_name');
        $crud->display_as('payment_id', '
Payment Code');
        $crud->display_as('payment_name', 'Jenis Pembayaran');

        $output = (array) $crud->render();
        $output['title'] = 'Payment Type list';

        $this->load->template('reservation/paymenttype', $output);
    }

    function addResv() {
        $data['css_files'] = false;
        $data['title'] = 'Add Reservasi';

        $this->load->template('reservation/add', $data);
    }

    function guestSearch() {
        $crud = new grocery_CRUD();

        $crud->set_table('guest');
        $crud->set_subject('Guest');
        $crud->required_fields('guest_name', 'guest_address', 'guest_phone', 'guest_group');
        $crud->columns('guest_name', 'guest_ktp', 'guest_address', 'guest_phone', 'guest_group', 'Pilih Guest');
        $crud->order_by('guest_id', 'desc');

        $crud->display_as('guest_name', 'Nama');
        $crud->display_as('guest_ktp', 'No. KTP');
        $crud->display_as('guest_address', 'Alamat');
        $crud->display_as('guest_phone', 'Telp.');
        $crud->display_as('guest_group', 'Jenis Guest');

        $crud->callback_field('guest_ktp', array($this, 'KTP_desc'));
        $crud->callback_column('Pilih Guest', array($this, 'chooseGuest'));

        $crud->unset_edit();
        $crud->unset_read();
        $crud->unset_delete();
        $crud->unset_export();
        $crud->unset_print();

        $output = $crud->render();
        $this->load->view('reservation/guestsearch', $output);
    }

    function KTP_desc($value = '') {
        return '<input type="number" maxlength="16" value="' . $value . '" name="guest_ktp" style="width:auto"> (Diisi jika bukan perusahaan)';
    }

    public function chooseGuest($value, $row) {
        return '<a class="btn btn-primary" href="javascript:void(0)" onclick="choose(\'' . $row->guest_id . '\', \'' . $row->guest_name . '\')">Pilih</a>';
    }

    function roomSearchAjax() {
        $this->load->model('reservation_model');

        if (!$_POST['checkin'])
            $_POST['checkin'] = 0;
        if (!$_POST['checkout'])
            $_POST['checkout'] = 0;
        if (!$_POST['adult'])
            $_POST['adult'] = 0;
        if (!$_POST['child'])
            $_POST['child'] = 0;
        if (!$_POST['infant'])
            $_POST['infant'] = 0;

        $data = $this->reservation_model->roomSearch($_POST['checkin'], $_POST['checkout'], $_POST['adult'], $_POST['child'], $_POST['infant']);

        echo json_encode($data);
    }

    function roomPriceAjax() {
        $this->load->model('reservation_model');

        $data = $this->reservation_model->roomPrice($_POST['checkin'], $_POST['checkout'], $_POST['room_number']);

        echo json_encode($data);
    }

    function save() {
        $session = $this->session->userdata('logged_in');


        $this->load->model('reservation_model');
        $this->reservation_model->saveResv($session['username']);
    }

    function edit($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Edit Reservation';

        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->getResv($resv_id);

        $this->load->template('reservation/edit', $output);
    }

    function editSave() {
        $this->load->model('reservation_model');
        $this->reservation_model->editResv();
    }

    function editRoom($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Room List';

        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->editRoom($resv_id);

        $this->load->template('reservation/resvroom', $output);
    }

    function service($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Service List';
        $output['resv_id'] = $resv_id;
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->listService($resv_id);

        $this->load->template('reservation/listService', $output);
    }

    function addServ($resv_id) {
        $output['css_files'] = false;
        $output['title'] = 'Add Service';
        $output['resv_id'] = $resv_id;
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->listServ($resv_id);

        $this->load->template('reservation/addServ', $output);
    }
    
    function saveServ(){
        $this->load->model('reservation_model');
        $this->reservation_model->addServ();
    }
    
    function editServ($id) {
        $output['css_files'] = false;
        $output['title'] = 'Edit Service List';
        
        $this->load->model('reservation_model');
        $output['data'] = $this->reservation_model->getServ($id);
$output['data2'] = $this->reservation_model->listServ();
        $this->load->template('reservation/editServ', $output);
    }
    
    function saveEditServ(){
        $this->load->model('reservation_model');
        $this->reservation_model->editServ();
    }
    
    function delServ($id) {
        $this->load->model('reservation_model');
        $this->reservation_model->deleteServ($id);
        redirect('services','refresh');
    }

    function pay() {
        
    }

}